package com.jcl.recruit.api.searchresume.controller;

import com.jcl.core.common.page.PageResult;
import com.jcl.core.constants.Constant;
import com.jcl.core.resp.ApiResponse;
import com.jcl.core.utils.CollectionUtils;
import com.jcl.recruit.core.common.CommonEnum;
import com.jcl.core.web.utils.RequestUtils;
import com.jcl.recruit.resume.service.ResumeAttentionService;
import com.jcl.recruit.resume.service.ResumeOrigService;
import com.jcl.recruit.searchresume.model.SearchResumeRecord;
import com.jcl.recruit.searchresume.service.SearchResumeRecordService;
import com.jcl.recruit.searchresume.service.SearchResumeService;
import com.jcl.recruit.searchresume.vo.ResumeSearchVo;
import com.jcl.recruit.server.elastic.SearchResumeServer;
import com.jcl.recruit.server.elastic.vo.SearchParamEntity;
import com.jcl.recruit.server.elastic.vo.SearchResumeRecordVo;
import com.jcl.recruit.server.elastic.vo.SearchResumeResultEntity;
import com.jcl.recruit.user.model.UserResumeAttention;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class SearchResumeController {

    @Autowired
    private SearchResumeRecordService searchResumeRecordService;

    @Autowired
    private ResumeAttentionService resumeAttentionService;

    @Autowired
    private ResumeOrigService resumeOrigService;

    @Autowired
    private SearchResumeServer searchResumeServer;

    @Autowired
    private SearchResumeService searchResumeService;

    /**
     * 推荐简历搜索
     *
     * @param searchVo
     * @return
     */
    @RequestMapping("/searchresume/recommend/find")
    public ApiResponse findRecommendResume(@RequestBody ResumeSearchVo searchVo) {

        Integer userId = Integer.parseInt(RequestUtils.getUserId());
        List<SearchResumeRecord> recordList = new ArrayList<>();
        if (null != searchVo.getRecommendId()) {
            SearchResumeRecord resumeRecord = searchResumeRecordService.findOne(
                    searchVo.getRecommendId());
            if (null != resumeRecord) {
                recordList.add(resumeRecord);
            }
        } else {
            recordList = searchResumeRecordService.findList(Constant.NUMBER_0);
        }
        if (CollectionUtils.isEmpty(recordList)) {
            PageResult result = new PageResult(searchVo.getToPage(), new ArrayList<>());
            return ApiResponse.success(result);
        } else {
            List<SearchResumeRecordVo> voList = new ArrayList<>();
            for(SearchResumeRecord record: recordList){
                SearchResumeRecordVo vo = new SearchResumeRecordVo();
                BeanUtils.copyProperties(record, vo);
                voList.add(vo);
            }
            SearchParamEntity entity = new SearchParamEntity();
            entity.setResumeRecords(voList);
            entity.setChannel(searchVo.getChannel());
            entity.setCurPage(searchVo.getToPage());
            entity.setCorpCode(RequestUtils.getCorpCode());
            PageResult<Map<String, Object>> result = searchResumeServer.resumePageList(entity);
            if (!CollectionUtils.isEmpty(result.getData())) {
                List<Integer> ids = new ArrayList<>();
                for (Map<String, Object> map : result.getData()) {
                    ids.add(Integer.parseInt(map.get("id").toString().split("\\.")[0]));
                    if (map.get("sourceSub") != null) {
                        map.put("sourceName", CommonEnum.ChannelStatus.
                                getName(map.get("sourceSub").toString()));
                    }
                }
                List<UserResumeAttention> attentions = resumeAttentionService.
                        getListBySearchResumeIds(userId, ids);
                List<String> attentionResumeIds = new ArrayList<>();
                if (!CollectionUtils.isEmpty(attentions)) {
                    for (UserResumeAttention attention : attentions) {
                        attentionResumeIds.add(attention.getSearchResumeId().toString());
                    }
                }
                for (Map<String, Object> map : result.getData()) {
                    if (attentionResumeIds.contains(map.get("id").toString())) {
                        map.put("attention", true);
                    } else {
                        map.put("attention", false);
                    }
                }
            }
            return ApiResponse.success(result);
        }
    }

    /**
     * 外部简历查询
     *
     * @param keyword
     * @param education
     * @param city
     * @param toPage
     * @return
     */
    @RequestMapping("/searchresume/find")
    public ApiResponse findSearchresume(String keyword,
                                        String education,
                                        String city,
                                        Integer workyear,
                                        Integer status,
                                        @RequestParam(value = "page", required = false, defaultValue = "1") Integer toPage) {
        if (StringUtils.isNotBlank(city) && city.equals("全国")) {
            city = null;
        }

        PageResult result = searchResumeServer.selectSearchResume(keyword, education, city, workyear, toPage);

        return ApiResponse.success(result);
    }

    @RequestMapping("/searchresume/get/{id}")
    public ApiResponse getSearchResume(@PathVariable("id") Integer id) {

        SearchResumeResultEntity entity = searchResumeService.findDetail(id);

        return ApiResponse.success(entity);
    }

    @RequestMapping("/statistics_daliy")
    public ApiResponse statisticsDaliy() {
        List<Map<String, String>> list = resumeOrigService.selectPrimarykeyByDatesAndSources();
        return ApiResponse.success("获取每日信息成功", list);
    }

    // 统计searchresume每日数据
    @RequestMapping("/search_resume/daliy")
    public ApiResponse searchResumeDaliy(String isToday) {

        Boolean today = StringUtils.isNotBlank(isToday) ? true : false;
        List<Map<String, String>> list = searchResumeServer.getCountBySources(today);
        return ApiResponse.success(list);
    }
}
